package com.mall.manager.power.service;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.WebUtils;

import com.mall.untils.Page;

/**权限控制街接口
 * @author zhaopeisen
*/
@Service("managepowerService")
public class ManagePowerService {

	    private Logger logger = LoggerFactory.getLogger(ManagePowerService.class);

	    @Resource
	    private JdbcTemplate jdbcTemplate;

		/**权限保存
		 * @param paraMap
		 * @return
		 */
		public int manager_power_save(Map<String, Object> paraMap) {
			int parent_id = MapUtils.getIntValue(paraMap, "parent_id");
			String name = MapUtils.getString(paraMap, "name");
			String poweraddr = MapUtils.getString(paraMap, "poweraddr");
			int level = MapUtils.getIntValue(paraMap, "level");
			String remarks = MapUtils.getString(paraMap, "remarks");
			int power_creator = MapUtils.getIntValue(paraMap, "power_creator");
			int power_editor = MapUtils.getIntValue(paraMap, "power_editor");
			try {
				String sql="insert into t_power(parent_id,name,poweraddr,level,remarks,power_creator,power_editor,create_time,update_time) values(?,?,?,?,?,?,?,now(),now())";
				System.out.println(jdbcTemplate);
				int update = jdbcTemplate.update(sql,parent_id,name,poweraddr,level,remarks,power_creator,power_editor);
				return update;
			} catch (Exception e) {
				logger.info("权限保存失败！");
				return 0;
			}
		}


		/**权限列表
		 * @param request
		 * @return
		 */
		public Page manager_power_list(HttpServletRequest request) {
 			Page page = null;
		    Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
		    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
		    String power_name = MapUtils.getString(paraMap, "power_name");
		    if(null==power_name){
		    	power_name = "";
		    }
		    int pageSize = 10;
	    	try {
	 	        if (pageNo == 0){
	 	        	pageNo = 1;
	 	        }
	 	        String sql = "select count(*) from t_power where name like '%"+power_name+"%'";
	 	        int total = this.jdbcTemplate.queryForInt(sql);
	 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
	 	        sql = "select * from t_power where name like '%"+power_name+"%' order by id desc limit " + startIndex + "," + pageSize;
	 	        List queryForList = this.jdbcTemplate.queryForList(sql);
	 	        page = new Page(startIndex, total, pageSize, queryForList);
	 	        return page;
	 	    } catch (Exception e) {
	 	        this.logger.info("权限列表获取失败！");
	 	        return null;
	 	    }
		}
		
		
		/**统计当前总页数
		 * @param request
		 * @return
		 */
		public int manager_totalPagecounts() {
		    int pageSize = 10;
	    	try {
	 	        String sql = "select count(*) from t_power";
	 	        int total = this.jdbcTemplate.queryForInt(sql);
	 	       if (total % pageSize == 0)
					return (int) total / pageSize;
				else
					return (int) total / pageSize + 1;
	 	    } catch (Exception e) {
	 	        this.logger.info("权限个数获取失败！");
	 	        return 0;
	 	    }
	    	
		}

		/**权限删除
		 * @param paraMap
		 * @return
		 */
		@Transactional
		public int manager_power_delete(Map<String, Object> paraMap) {
			int id = MapUtils.getIntValue(paraMap, "id");
			try {
				String sql="select count(*) from t_power where parent_id=?";
				int count = jdbcTemplate.queryForInt(sql,id);
				if(count>0){
					return -1;
				}
				String Dsql="delete from t_power where id=?";
				//删除权限表
				jdbcTemplate.update(Dsql,id);
				String Msql = "delete from t_power_part where power_id=?";
				//删除权限角色关联表
				jdbcTemplate.update(Msql,id);
				return 1;
			} catch (Exception e) {
				logger.info("权限删除失败！");
				return 0;
			}
		}
		
		/**按照Id查找权限
		 * @param paraMap
		 * @return
		 */
		public Map<String, Object> manager_power_findone(Map<String, Object> paraMap) {
			int id = MapUtils.getIntValue(paraMap, "id");
			try {
				String sql="select * from t_power where id=?";
				Map<String, Object> queryForMap = jdbcTemplate.queryForMap(sql,id);
				return queryForMap;
			} catch (Exception e) {
				logger.info("权限查找失败！");
				return null;
			}
		}
		
		/**修改权限
		 * @param paraMap
		 * @return
		 */
		public int manager_power_update(Map<String, Object> paraMap) {
			String id = MapUtils.getString(paraMap, "id");
			int parent_id = MapUtils.getIntValue(paraMap, "parent_id");
			String name = MapUtils.getString(paraMap, "name");
			String poweraddr = MapUtils.getString(paraMap, "poweraddr");
			int level = MapUtils.getIntValue(paraMap, "level");
			String remarks = MapUtils.getString(paraMap, "remarks");
			int power_editor = MapUtils.getIntValue(paraMap, "power_editor");
			try {
				String sql="update t_power set parent_id=?,name=?,poweraddr=?,level=?,remarks=?,power_editor=?,update_time=now() where id=?";
				int update = jdbcTemplate.update(sql,parent_id,name,poweraddr,level,remarks,power_editor,id);
				return update;
			} catch (Exception e) {
				logger.info("权限修改失败！");
				return 0;
			}
		}
	
		/**获取？level的所有权限
		 * @param paraMap
		 * @return
		 */
		public 	List<Map<String, Object>> manager_getallpowers1(Map<String, Object> paraMap) {
				int level = MapUtils.getInteger(paraMap, "level");
		    	try {
		    		String sql = "";
		    		if(level== 0){
			   	    	  return null;
			   	   	}else{  	    
			   	   		sql = "select id,parent_id,name,poweraddr,level,remarks from t_power where level=?";
			   	   	}
		 	       	List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql,level);
		 	        return queryForList;
		 	    } catch (Exception e) {
		 	        this.logger.info("获取该level权限权限失败！");
		 	        return null;
		 	    }
			}
	}
